import Vue from 'vue'
import VueRouter from 'vue-router'
const login =()=>import("../components/Login.vue")
const home =()=>import("../components/Home.vue")
const userInfo =()=>import("../components/UserInfo.vue")
const users =()=>import("../components/user/Users.vue")
const rights =()=>import("../components/power/Rights.vue")
const roles =()=>import("../components/power/Roles.vue")
const cate =()=>import("../components/goods/Cate.vue")
const params =()=>import("../components/goods/params.vue")
const goods =()=>import("../components/goods/List.vue")
const add =()=>import("../components/goods/Add.vue")
const order =()=>import("../components/order/Order.vue")
const report =()=>import("../components/report/Report.vue")

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: "/login" },
  { path: '/login', component: login },
  {
    path: '/home', component: home, redirect:'/welcome',
    children: [
      { path: "/welcome", component: userInfo },
      { path: '/users', component: users},
      { path: '/rights', component: rights},
      { path: '/roles', component: roles},
      { path: '/categories', component: cate},
      { path: '/params', component: params},
      { path: '/goods', component: goods},
      { path: '/goods/add', component: add},
      { path: '/orders', component: order},
      { path: '/reports', component: report},
    ]
  },
]

const router = new VueRouter({
  routes
})

//挂载路由守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 代表从哪个路径跳转而来
  //next 是一个函数 表示放行

  if (to.path === '/login') return next();   //登陆不需要权限 直接放行
  //获取token
  const tokenStr = window.sessionStorage.getItem("token")
  if (!tokenStr) return next('/login');
  //正常
  next();
})

export default router
